package com.lhx.product.controller;

import com.github.pagehelper.PageHelper;
import com.lhx.common.core.domain.R;
import com.lhx.product.domain.dto.ProductCategoryDTO;
import com.lhx.product.domain.dto.ProductCategoryWithChildrenItem;
import com.lhx.product.domain.po.ProductCategoryPO;
import com.lhx.product.result.PageResult;
import com.lhx.product.domain.vo.ProductCategoryVO;

import com.lhx.product.service.ProductCategoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author：李洪祥
 * @Package：com.lhx.product.controller
 * @Project：lhx-RuoYi
 * @name：ProductCategoryController
 * @Date：2024/5/23 下午7:11
 */
@RestController
@RequestMapping("/productCategory")
@Api(tags = "商品管理模块")
public class ProductCategoryController {

    @Autowired
    private ProductCategoryService productCategoryService;

    /**
     * 分页查询商品分类列表
     *
     * @param productCategoryDTO
     * @return
     */
    @ApiOperation(value = "分页查询商品分类列表")
    @PostMapping("/findList")
    public R<PageResult<ProductCategoryVO>> findList(@RequestBody ProductCategoryDTO productCategoryDTO) {
        //查询所有
        List<ProductCategoryVO> list = productCategoryService.findList(productCategoryDTO);
        //分页
        PageHelper.startPage(productCategoryDTO.getPageNum(), productCategoryDTO.getPageSize());
        //返回
        return R.ok(PageResult.toPageResult(list.size(), list));
    }


    /**
     * 添加商品分类
     *
     * @param productCategoryPO
     * @return
     */
    @ApiOperation("添加商品分类")
    @PostMapping("/create")
    public R create(@Validated @RequestBody ProductCategoryPO productCategoryPO) {
        int count = productCategoryService.create(productCategoryPO);
        if (count > 0) {
            return R.ok(count, "添加成功");
        } else {
            return R.fail();
        }
    }

    /**
     * 修改商品分类
     *
     * @param id
     * @param productCategoryPO
     * @return
     */

    @ApiOperation("修改商品分类")
    @PostMapping(value = "/update/{id}")
    public R update(@PathVariable Long id, @Validated @RequestBody ProductCategoryPO productCategoryPO) {
        int count = productCategoryService.update(id, productCategoryPO);
        if (count > 0) {
            return R.ok(count, "修改成功");
        } else {
            return R.fail();
        }
    }

    /**
     * 根据id获取商品分类
     *
     * @param id
     * @return
     */

    @ApiOperation("根据id获取商品分类")
    @GetMapping(value = "/{id}")
    public R<ProductCategoryVO> getItem(@PathVariable Long id) {
        ProductCategoryVO productCategory = productCategoryService.getItem(id);
        return R.ok(productCategory);
    }

    /**
     * 删除商品分类
     *
     * @param id
     * @return
     */
    @ApiOperation("删除商品分类")
    @PostMapping(value = "/delete/{id}")
    public R delete(@PathVariable Long id) {
        int count = productCategoryService.delete(id);
        if (count > 0) {
            return R.ok(count);
        } else {
            return R.fail();
        }
    }

    /**
     * 修改导航栏显示状态
     *
     * @param ids
     * @param navStatus
     * @return
     */

    @ApiOperation("修改导航栏显示状态")
    @PostMapping(value = "/update/navStatus")
    public R updateNavStatus(@RequestParam("ids") List<Long> ids, @RequestParam("navStatus") Integer navStatus) {
        int count = productCategoryService.updateNavStatus(ids, navStatus);
        if (count > 0) {
            return R.ok(count);
        } else {
            return R.fail();
        }
    }

    /**
     * 修改显示状态
     *
     * @param ids
     * @param showStatus
     * @return
     */
    @ApiOperation("修改显示状态")
    @RequestMapping(value = "/update/showStatus", method = RequestMethod.POST)
    @ResponseBody
    public R updateShowStatus(@RequestParam("ids") List<Long> ids, @RequestParam("showStatus") Integer showStatus) {
        int count = productCategoryService.updateShowStatus(ids, showStatus);
        if (count > 0) {
            return R.ok(count);
        } else {
            return R.fail();
        }
    }

    /**
     * 查询所有一级分类及子分类
     * @return
     */
    @ApiOperation("查询所有一级分类及子分类")
    @GetMapping(value = "/list/withChildren")
    public R<List<ProductCategoryWithChildrenItem>> listWithChildren() {
        List<ProductCategoryWithChildrenItem> list = productCategoryService.listWithChildren();
        return R.ok(list);
    }


}
